package dao;

import Console.Publisher;
import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Account;
import pojo.Action;
import pojo.Equipment;
import pojo.Mail;
import pojo.Player;

/**
 *
 * @author Kenneth Veenstra
 */
public class AccountDao
{
    private Session session;

    public AccountDao()
    {
    }

    @SuppressWarnings("unchecked")
    public List<Account> getAccounts()
    {
        session = HibernateUtil.getSessionFactory().openSession();
        org.hibernate.Transaction tx = null;
        List<Account> list = null;
        try
        {
            tx = session.beginTransaction();
            Query q = session.createQuery("from Account as Account");
            list = (List<Account>) q.list();
            tx.commit();
        }
        catch ( Exception e )
        {
            if ( tx != null )
            {
                tx.rollback();
            }
            Publisher.getInstance().log("ERROR: AccountDao.getAccounts() -> Exception : "+e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            session.close();
        }
        return list;
    }

    public Account getAccountByName(String name)
    {
        session = HibernateUtil.getSessionFactory().openSession();
        org.hibernate.Transaction tx = null;
        Account account = null;
        try
        {
            tx = session.beginTransaction();
            account = (Account) session.createQuery("From Account where name = '" + name + "'").uniqueResult();
            tx.commit();
        }
        catch ( Exception e )
        {
            if ( tx != null )
            {
                tx.rollback();
            }
            Publisher.getInstance().log("ERROR: AccountDao.GetAccountByName() -> Exception : "+e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            session.close();
        }
        return account;
    }

    public boolean addAccount(Account account)
    {
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try
        {
            Account check = (Account) session.createQuery("From Account where name = '" + account.getName() + "'").uniqueResult();
            if ( check != null )
            {
                session.close();
                return false;
            }
            tx = session.beginTransaction();
            session.save(account);
            session.save(new Action());
            session.save(new Equipment());
            session.save(new Player(0, 5000, 50000, 0, 100, 0, false, false));
            tx.commit();
        }
        catch ( HibernateException e )
        {
            if ( tx != null )
            {
                tx.rollback();
            }
            Publisher.getInstance().log("ERROR: AccountDao.addAccount() -> Exception : "+e.getMessage());
            e.printStackTrace();
            return false;
        }
        finally
        {
            session.close();
        }
        return true;
    }

    public boolean updateAccount(Account account)
    {
        session = HibernateUtil.getSessionFactory().openSession();
        org.hibernate.Transaction tx = null;
        try
        {
            tx = session.beginTransaction();
            Account newAccount = (Account) session.get(Account.class, account.getId());
            newAccount.setChanges(account);
            session.update(newAccount);
            tx.commit();
        }
        catch ( Exception e )
        {
            if ( tx != null )
            {
                tx.rollback();
            }
            Publisher.getInstance().log("ERROR: AccountDao.updateAccount() -> Exception : "+e.getMessage());
            e.printStackTrace();
            return false;
        }
        finally
        {
            session.close();
        }
        return true;
    }
}
